#!/bin/sh
# Copyright (c) 2010, bkil.hu
# This program is free software and can be distributed under the terms of
# the GNU General Public License v2,
# see COPYING for detailed licensing terms.

# created on 2010-10-30 13:00

#echo $(pidof $(basename $0 ) ) # ||exit 1

getchan() {
 if tvtime-command NOOP >/dev/null 2>/dev/null ;then
  grep '<option name="Channel" value="' ~/.tvtime/tvtime.xml |
  sed 's~^.*<option name="Channel" value="\([^"]*\)"/>.*~\1~'
 else
  echo -
 fi
}

LAST="-"
while true
do
 CHAN=`getchan`
 if [ "$CHAN" != "$LAST" ] ;then
  echo $CHAN
  LAST=$CHAN
  if [ "$CHAN" = "22" ] || [ "$CHAN" = "13" ] ;then
   tvtime-command DISPLAY_MESSAGE "pillanat, 5 masodperc es bejon..."
   sleep 1
   CHAN=`getchan`
   if [ "$CHAN" = "$LAST" ] ;then
    tvtime-command TOGGLE_PAL_SECAM
    tvtime-command TOGGLE_PAL_SECAM
    sleep 0.1
   fi
   tvtime-command DISPLAY_MESSAGE ""
  fi
 fi
 sleep 1
done
